<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/6/18
 * Time: 13:39
 */

namespace app\admin\controller;

use think\Request;

class Category extends Base
{
    protected $model=null;

    public function __construct(Request $request = null)
    {
        $this->model=new \app\admin\model\Category();
    }

    /**
     * 分类列表
     * @param page
     * @param pagenum
     * @return \think\response\Json
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function index(Request $request){
        if ($request->isGet()){
            $page=$request->param('page');
            $pagenum=$request->param('pagenum');
            $model=$this->model;
            $obj=$model->where('isshow','=',1)->page($page,$pagenum)->select();
            $total=$model->where('isshow','=',1)->count();
            $collect=collection($obj);
            $arr=$collect->toArray();

            $arr['total']=$total;
            $arr['pagenum']=$pagenum;
            $arr['page']=$page;

            if($total>0){
                return json(['status'=>200,'msg'=>'获取成功','data'=>$arr]);
            }else{
                return json(['status'=>200,'msg'=>'获取成功','data'=>[]]);
            }

        }else{
            return json(['status'=>404,'msg'=>'非法请求']);
        }
    }

    /**
     * 分类添加
     * @param title String 分类名称
     * @param linkto String 跳转地址
     * @param Request $request
     * @return \think\response\Json
     */
    public function save(Request $request){
        if($request->isPost()){
            $data['title']=$request->post('title');
            $data['linkTo']=$request->post('linkTo');
            $model=$this->model;

            $bool=$model->data($data)->save();

            if($bool){
                return json(['status'=>201,'msg'=>'分类添加成功']);
            }else{
                return json(['status'=>400,'msg'=>'分类添加成功']);
            }
        }else{
            return json(['status'=>500,'msg'=>'非法请求']);
        }
    }

    /**
     * 分类删除
     * @param id int 删除分类的主键id
     * @return \think\response\Json
     */
    public function del(Request $request){
        if($request->isDelete()){
            $id=$request->param('id');
            $model=$this->model;
            $bool=$model->where('id','=',$id)->delete();

            if($bool){
                return json(['status'=>204,'分类删除成功']);
            }else{
                return json(['status'=>400,'没有该分类']);
            }
        }else{
            return json(['status'=>404,'msg'=>'非法请求']);
        }
    }

    /**
     * 分类编辑
     * @param id int 分类表主键id
     * @param title 分类标题
     * @param linkTo 跳转地址
     * @return \think\response\Json
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function edit(Request $request){
        if($request->isPut()){
            $id=$request->param('id');  //获取修改数据的id
            $data=$request->put();              //获取提交的修改数据
            $model=$this->model;                //初始化模型
            $bool=$model->allowField(true)->save($data,['id' => $id]);    //数据入库
            if($bool){
                $category=$model->where('id','=',$id)->find();      //查询修改过后的数据
                $data=$category->toArray();                                     //数据转换为数组
                return json(['status'=>'200','msg'=>'数据修改成功','data'=>$data]);
            }else{
                return json(['status'=>'400','msg'=>'数据修改失败'.$model->getError()]);
            }
        }else{
            return json(['status'=>404,'msg'=>'非法请求']);
        }
    }

    /**
     * 分类搜索方法
     * @param title String 搜索的标题
     * @return \think\response\Json
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function search(Request $request){
        if($request->isPost()){
            $title=$request->param('title');
            $model=$this->model;

            $res=$model->where('title','like','%'.$title.'%')->find();

            if($res){
                return json(['status'=>200,'msg'=>'查询成功','data'=>$res->toArray()]);
            }else{
                return json(['status'=>400,'msg'=>'没有查询结果']);
            }
        }else{
            return json(['status'=>404,'msg'=>'非法请求']);
        }
    }

}